const { get_fst_file } = require("../utils/file_tool");
const { BaseCommand } = require("./base");
const fs = require("node:fs");
const mammoth = require("mammoth");

class Word2txtCommand extends BaseCommand {
    async execute() {
        const data = this.selfData;
        const wordFilePath = get_fst_file(data.src);
        return new Promise((resolve, reject) => {
            // 读取 Word 文件
            fs.readFile(wordFilePath, (err, data) => {
                if (err) {
                    reject(err);
                    return console.error("读取文件时出错:", err);
                }

                // 将 Word 文件转换为纯文本
                mammoth
                    .extractRawText({
                        buffer: data,
                    })
                    .then((result) => {
                        const text = result.value; // 纯文本内容
                        console.log("转换后的文本内容:", text);
                        resolve(text);
                    })
                    .catch((err) => {
                        console.error("转换过程中出错:", err);
                        reject(err);
                    });
            });
        });
    }
}

module.exports = {
    Word2txtCommand,
};
